# [1] user project src
file(GLOB_RECURSE USER_SRC ${CMAKE_CURRENT_SOURCE_DIR}/*.c)

# [2] KV
file(GLOB_RECURSE KV_SRC        ${COMP_KV_DIR}/*.c)

# [3] utils
set(UTILS_SRC
    ${COMP_UTILS_DIR}/debug/CmBacktrace/cm_backtrace.c
    ${COMP_UTILS_DIR}/debug/art_assert.c
    ${COMP_UTILS_DIR}/debug/log.c
    ${COMP_UTILS_DIR}/fifo/fifobuf.c
    ${COMP_UTILS_DIR}/reboot_trace/reboot_trace.c
    ${COMP_UTILS_DIR}/runtime/runtime.c
    ${COMP_UTILS_DIR}/art_string.c
    ${COMP_UTILS_DIR}/crc32.c
    ${COMP_UTILS_DIR}/wrap_stdio.c
)

# [4] serial
file(GLOB_RECURSE  SERIAL_SRC  ${COMP_SERIAL_DIR}/*.c)

# [5] phony
set(PHONY_SRC
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/cache.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/flash.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_adc.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_aes.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_common.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_dma.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_efuse.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_gpio.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_i2c.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_i2s.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_interrupt.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_rtc.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_syscon.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_timer.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_trng.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_uart.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/hal_wdt.c
    ${MCU_LN882X_DIR}/driver_ln882x/src/hal/qspi.c
    ${MCU_LN882X_DIR}/ln88xx/system_ln88xx.c
    ${CMAKE_SOURCE_DIR}/components/libc/newlib_noos.c
    ${CMAKE_SOURCE_DIR}/components/libc/stub_malloc.c
)

list(APPEND PRO_SRC
    ${USER_SRC}
    ${KV_SRC}
    ${UTILS_SRC}
    ${SERIAL_SRC}
    ${PHONY_SRC}
    ${KERNEL_SRC}
)

# NOTE: third-party library searching path.
link_directories(${CMAKE_SOURCE_DIR}/lib/gcclib)

# NOTE: header searching path.
include_directories(
    ${MCU_LN882X_DIR}/ln88xx
    ${MCU_LN882X_DIR}/CMSIS_5.3.0
    ${COMP_KV_DIR}/kv
    ${COMP_KV_DIR}/kv_port
    ${COMP_DHCPD_DIR}
    ${COMP_LWIP_DIR}/include
    ${COMP_LWIP_DIR}/port
    ${COMP_LWIP_DIR}/port/arch
    ${CMAKE_SOURCE_DIR}/components
    ${CMAKE_SOURCE_DIR}/components/libc
    ${MCU_LN882X_DIR}/driver_ln882x/inc
    ${MCU_LN882X_DIR}/driver_ln882x/inc/hal
    ${COMP_SERIAL_DIR}
)

################################################################################
#####################   update flash partition table first  ####################
################################################################################
set(FLASH_PART_CFG_JSON    ${CMAKE_SOURCE_DIR}/project/${USER_PROJECT}/cfg/flash_partition_cfg.json)
set(FLASH_PART_TABLE_FILE  ${CMAKE_SOURCE_DIR}/project/${USER_PROJECT}/cfg/flash_partition_table.h)
add_custom_target(create_flash_table
    COMMAND python ${CMAKE_SOURCE_DIR}/tools/user_cmd/before_build.py ${FLASH_PART_CFG_JSON} ${FLASH_PART_TABLE_FILE}
    WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/project/${USER_PROJECT}/cfg
    COMMENT "--->  update flash_partition_table.h for ${USER_PROJECT}  <---"
)

################################################################################
#############################   final target   #################################
################################################################################
set(pro_executable_target  ${TARGET_ELF_NAME})
add_executable(${pro_executable_target}  ${PRO_SRC})
add_dependencies(${pro_executable_target}
    create_flash_table
)

target_link_libraries(${pro_executable_target}
    PUBLIC
    driver_ln882x
    -lc -lm -lnosys

    PRIVATE
    -T${LINKER_SCRIPT}
    ${EXTRA_LINK_FLAGS}
)

target_include_directories(${pro_executable_target}
    PRIVATE
    bsp
    app
    cfg
)

include(${CMAKE_SOURCE_DIR}/project/${USER_PROJECT}/gcc/gcc-postbuild.cmake)
